VPATH=lib lib/kernel kernel device thread \
device/key userprog
AS=nasm
CC=gcc
BUILD_DIR=build/
C_INC=-I lib/kernel -I lib -I kernel -I device -I thread \
-I device/key -I userprog
BOOT_DIR=boot/
CFLAGS=-Wall -m32 $(C_INC) -c -fno-stack-protector \
-fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes
ASFLAGS=-f elf
LDFLAGS=-m elf_i386 -Ttext 0xc0001500 -e main

img:mbr.bin loader.bin kernel.bin
	dd if=$(BUILD_DIR)mbr.bin of=hd60m.img bs=512 count=1 seek=0 conv=notrunc
	dd if=$(BUILD_DIR)loader.bin of=hd60m.img bs=512 count=4 seek=2 conv=notrunc
	dd if=$(BUILD_DIR)kernel.bin of=hd60m.img bs=512 count=200 seek=9 conv=notrunc
	# dd if=$(BUILD_DIR)mbr.bin of=fd144.img bs=512 count=1 seek=0 conv=notrunc
	# dd if=$(BUILD_DIR)loader.bin of=fd144.img bs=512 count=4 seek=2 conv=notrunc
	# dd if=$(BUILD_DIR)kernel.bin of=fd144.img bs=512 count=200 seek=9 conv=notrunc


mbr.bin:$(BOOT_DIR)mbr.asm
	$(AS) -I $(BOOT_DIR) -o $(BUILD_DIR)$@ $<
loader.bin:$(BOOT_DIR)loader.asm
	$(AS) -I $(BOOT_DIR) -o $(BUILD_DIR)$@ $<


kernel.bin:main.o print.o kernel.o init.o interrupt.o \
timer.o debug.o string.o bitmap.o memory.o thread.o list.o \
switch.o sync.o console.o keyboard.o ioqueue.o tss.o process.o
	ld $(LDFLAGS) -o $(BUILD_DIR)$@ \
	$(foreach n, $^,$(BUILD_DIR)$n)
main.o: main.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
print.o:print.asm
	$(AS) $(ASFLAGS) -o $(BUILD_DIR)$@ $<
kernel.o:kernel.asm
	$(AS) $(ASFLAGS) -o $(BUILD_DIR)$@ $<
switch.o:switch.asm
	$(AS) $(ASFLAGS) -o $(BUILD_DIR)$@ $<
init.o:init.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
interrupt.o:interrupt.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
timer.o:timer.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
debug.o:debug.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
string.o:string.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
bitmap.o:bitmap.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
memory.o:memory.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
thread.o:thread.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
list.o:list.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
sync.o:sync.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
console.o:console.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
keyboard.o:keyboard.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
ioqueue.o:ioqueue.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
tss.o:tss.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<
process.o:process.c
	$(CC) $(CFLAGS) -o $(BUILD_DIR)$@ $<

.PHONY:clear
clear:
	rm ./build/*.o